//
// StoreNuke.Net -  http://www.storenuke.net
// Copyright (c) 2005
// by Robert J Collins ( info@storenuke.net ) of WillowTree Software, Inc. ( http://www.willowtreesoftware.com )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

namespace StoreNuke.StarterKit.Commerce.StoreControls
{
	/// <summary>
	///		Summary description for CategoryAdmin.
	/// </summary>
	public class CategoryAdmin : AdminControls
	{
		protected LinkButton btnNewCat;
		protected Label lblNavSpacer;
		protected LinkButton btnEditCat;
		protected Panel pnNewCat;
		protected Panel pnEditCat;
		protected LinkButton btnSave;
		protected LinkButton btnCacel;
		protected TextBox txtName;
		protected Label lblName;
		protected Label lblDescription;
		protected Label lblSpacer2;
		protected LinkButton btnDelete;
		protected TextBox txtDesc;
		protected Label lblSpacer3;
		protected DataGrid dgEditCategory;
		protected ProductsDB cat = new ProductsDB();
		protected int cID = 0;
		protected RequiredFieldValidator rfvName;
		protected RequiredFieldValidator rfvDesc;
		protected System.Web.UI.WebControls.Label lblError;
		protected System.Web.UI.WebControls.CheckBox cbArchived;
		protected string redirectURL;

		private void Page_Load(object sender, EventArgs e)
		{
			redirectURL = "Default.aspx?scid=" + StoreControlID.ToString() + "&sctl=1";

			ControlTitle = "Category Admin";

			if(Request.QueryString["cid"] != null)
			{
				cID = Convert.ToInt32(Request.QueryString["cid"]);
			}

			if(! Page.IsPostBack)
			{
				if(Request.QueryString["cid"] != null)
				{
					//Add delete confirmation pop-up
					SetEditVisibility(true);
					SetPanelVisibility(false);
					Utilities.GetDeleteConfirmationScriptLinkButton(btnDelete,"");
					GetCategoryInfo(cID);
				}
				else
				{
					SetEditVisibility(false);
				}
				FillDataGrid();
			}
		}

		private void FillDataGrid()
		{
			dgEditCategory.DataSource = cat.GetAllProductCategories();
			dgEditCategory.DataBind();
		}

		private void SetEditVisibility(bool editMode)
		{
			btnDelete.Visible = editMode;
			lblSpacer3.Visible = editMode;
			FillDataGrid();
		}

		private void SetPanelVisibility(bool editMode)
		{
			pnNewCat.Visible = (! editMode);
			pnEditCat.Visible = editMode;
			ClearTextboxes();
			FillDataGrid();
		}

		private void ClearTextboxes()
		{
			txtName.Text = "";
			txtDesc.Text = "";
		}

		public string GetEditURL(string CategoryID)
		{
			return "../" + redirectURL + "&cid=" + CategoryID;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnNewCat.Click += new System.EventHandler(this.btnNewCat_Click);
			this.btnEditCat.Click += new System.EventHandler(this.btnEditCat_Click);
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			this.btnCacel.Click += new System.EventHandler(this.btnCacel_Click);
			this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnNewCat_Click(object sender, System.EventArgs e)
		{
			SetPanelVisibility(false);
			lblError.Visible = false;
		}

		private void btnEditCat_Click(object sender, System.EventArgs e)
		{
			SetPanelVisibility(true);
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			if(cID == 0)
			{
				cat.AddCategory(txtName.Text, txtDesc.Text, cbArchived.Checked);
				ClearTextboxes();
			}
			else
			{
				cat.UpdateCategory(cID, txtName.Text, txtDesc.Text, cbArchived.Checked);
				RedirectPage();
			}
		}

		private void btnCacel_Click(object sender, System.EventArgs e)
		{
			RedirectPage();
		}

		private void btnDelete_Click(object sender, System.EventArgs e)
		{
			int message = 0;
			try
			{
				message = cat.DeleteCategory(cID);
			}
			catch
			{
				lblError.Visible = true;
				lblError.Text = "You cannot delete a category that has products assigned to it!";
			}

			if(message > 0)
			{
				lblError.Visible = true;
				lblError.Text = "You cannot delete a category that has products assigned to it!";
			}
			else
			{
				RedirectPage();
			}
		}

		private void RedirectPage()
		{
			Response.Redirect(redirectURL);
		}

		private void GetCategoryInfo(int cid)
		{
			SqlDataReader dr = cat.GetSingleProductCategory(cid);
			if(dr.Read())
			{
				txtName.Text = dr["CategoryName"].ToString();
				txtDesc.Text = dr["CategoryDescription"].ToString();
				cbArchived.Checked = (bool)dr["Archived"];
			}
			dr.Close();
		}
	}
}
